<?php


class post
{
    public static function login():bool
    {
        $username = argsTool::post("username");
        $password = hash("sha256",argsTool::post("password"));
        $result = dbTool::select("user",["*"],["*"]);
        switch ($result["status"]){
            case "success":
                $exist = false;
                foreach ($result["data"] as $datum) {
                    if ($datum["username"]==$username or $datum["email"]==$username){
                        $exist = true;
                        if ($datum["password"]==$password){
                            if ($datum["is_ban"]=="yes"){
                                echo json_encode(["status"=>"is-ban","reason"=>$datum["ban_reason"]]);
                            }else{
                                echo json_encode(["status"=>"success"]);
                                cookie::set("isLogin","yes")
                                    ->set("username",$datum["username"])
                                    ->set("nickname",$datum["nickname"])
                                    ->set("room_xcid","chat")
                                    ->set("password-part1", substr($password, 0, 20))
                                    ->set("password-part2", substr($password, 20, 20))
                                    ->set("password-part3", substr($password, 40, 20))
                                    ->set("password-part4", substr($password, 60, 4))
                                    ->set("isAdmin",$datum["is_admin"]);
                            }
                        }else{
                            echo json_encode(["status"=>"password-wrong"]);
                        }
                    }
                }
                if (!$exist){
                    echo json_encode(["status"=>"user-not-exist"]);
                }
                break;
            case "fail":
                echo json_encode(["status"=>"fail","reason"=>$result["reason"]]);
        }
        return true;
    }

    public static function dbAdmin():bool
    {
        dbAdmin::run();
        return true;
    }
}